package com.zhang.common.core;

import com.zhang.common.core.utils.jwt.JwtUtils;
import com.zhang.common.core.utils.uuid.SeqUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * @Class com.zhang.common.core.utils
 * @Author zhang
 * @Date 2025/9/11 9:44
 * @Description 测试工具类
 */
public class Test {

    /**
     * 测试工具实现方法
     *
     * @param args 参数数据集
     */
    public static void main(String[] args) {

        // uuid
        uuidTest();



    }

    private static void uuidTest() {
        String uuid = SeqUtils.getId();
        System.out.println("uuid = " + uuid);

        String randomAlphanumeric = RandomStringUtils.randomAlphanumeric(64);
        System.out.println("randomAlphanumeric = " + randomAlphanumeric);
        byte[] encode = Base64.getEncoder().encode(randomAlphanumeric.getBytes());
        System.out.println("encode = " + new String(encode));
        byte[] decode = Base64.getDecoder().decode(encode);
        System.out.println("decode = " + new String(decode));
    }


    /**
     * JWT 令牌测试
     *
     */
    private static void jwtTest() {

        // 生成随机 64 位字符串
        String randomAlphanumeric1 = RandomStringUtils.randomAlphanumeric(64);
        String randomAlphanumeric2 = RandomStringUtils.randomAlphanumeric(64);
        // base64 加密
        byte[] encode1 = Base64.getEncoder().encode(randomAlphanumeric1.getBytes());
        byte[] encode2 = Base64.getEncoder().encode(randomAlphanumeric2.getBytes());
        String secret1 = new String(encode1);
        String secret2 = new String(encode2);

        // ===================== 初始化 ===============================
        // 使用默认配置 (1小时访问令牌，7天刷新令牌)
        JwtUtils jwtUtil = new JwtUtils(secret1);

        // 自定义配置
        JwtUtils customJwtUtil = new JwtUtils(
                secret2,
                "custom-blog",
                1800,  // 30分钟访问令牌
                259200 // 3天刷新令牌
        );


        // ================= 生成令牌 ================================
        // 生成基本访问令牌
        String accessToken1 = jwtUtil.generateAccessToken("user123");
        String accessToken2 = customJwtUtil.generateAccessToken("user123");
        System.out.println("生成基本访问令牌 - accessToken1: {}" + accessToken1);
        System.out.println("生成基本访问令牌 - accessToken2: {}" + accessToken2);



        // 生成带自定义声明的访问令牌
        Map<String, Object> claims = Map.of(
                "roles", List.of("ADMIN", "USER"),
                "email", "user@example.com"
        );
        String customToken1 = jwtUtil.generateAccessToken("user123", claims);
        String customToken2 = customJwtUtil.generateAccessToken("user123", claims);
        System.out.println("生成带自定义声明的访问令牌 - customToken1: {}" + customToken1);
        System.out.println("生成带自定义声明的访问令牌 - customToken2: {}" + customToken2);

        // 生成刷新令牌
        String refreshToken1 = jwtUtil.generateRefreshToken("user123");
        String refreshToken2 = customJwtUtil.generateRefreshToken("user123");
        System.out.println("生成刷新令牌 - refreshToken1: {}" + refreshToken1);
        System.out.println("生成刷新令牌 - refreshToken2: {}" + refreshToken2);

        // ===================== 解析和验证令牌 ===============================

        // 安全解析
        Claims safeClaims1 = jwtUtil.safeParse(accessToken1);
        if (safeClaims1 != null) {
            System.out.println("安全解析 - 用户: " + safeClaims1.getSubject() +
                    ", 角色: {}" + safeClaims1.get("roles"));
        }

        Claims safeClaims2 = customJwtUtil.safeParse(accessToken2);
        if (safeClaims2 != null) {
            System.out.println("安全解析 - 用户: " + safeClaims2.getSubject() +
                    ", 角色: {}" + safeClaims2.get("roles"));
        }

        // 验证令牌
        boolean isValid1 = jwtUtil.validateToken(accessToken1);
        boolean isValid2 = customJwtUtil.validateToken(accessToken2);
        System.out.println("验证令牌: " + isValid1 + ", " + isValid2);
        boolean isValidForUser1 = jwtUtil.validateToken(accessToken1, "user123");
        boolean isValidForUser2 = customJwtUtil.validateToken(accessToken2, "user123");
        System.out.println("验证令牌用户: " + isValidForUser1 + ", " + isValidForUser2);

        // 提取特定信息
        String username1 = jwtUtil.extractUsername(accessToken1);
        String username2 = customJwtUtil.extractUsername(accessToken2);
        System.out.println("从令牌中提取用户名: " + username1 + ", " + username2);
        long date1 = jwtUtil.extractExpiration(accessToken1).getTime();
        long date2 = customJwtUtil.extractExpiration(accessToken2).getTime();
        System.out.println("从令牌中提取过期时间: " + date1 + ", " + date2);
    }

}